package com.teamcalendar.client.controller;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.teamcalendar.client.DBAdapter;

public class MemberOverviewActivity extends ListActivity {

	DBAdapter mDb = new DBAdapter(this);
	Bundle bundle;
	String path;
	String memberId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_overview);
		mDb.open();
		bundle = getIntent().getExtras();
		path = bundle.getString("path");
		listAllMyTeamMembers();
		if (path.equals("myTeam")) {
			memberId = bundle.getString("memberId");
			registerForContextMenu(getListView());
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mDb.close();
	};

	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent memberSite = new Intent(MemberOverviewActivity.this, MemberInformationActivity.class);
		memberSite.putExtra("memberRowId", id);
		memberSite.putExtra("path", path);
		memberSite.putExtra("memberId", memberId);
		startActivity(memberSite);
	}
	private void listAllMyTeamMembers() {

		ListView lv = (ListView) findViewById(android.R.id.list);
		Cursor c;
		if(path.equals("myTeam")){
			c = mDb.getAllMyTeamMembers();
		} else {
			c = mDb.getAllCurrentTeamMembers();
		}
		startManagingCursor(c);

		String[] from = new String[]{DBAdapter.KEY_FIRSTNAME,  DBAdapter.KEY_LASTNAME};
		int[] to = new int[] { R.id.member_firstname, R.id.member_lastname};

		SimpleCursorAdapter members = 
			new SimpleCursorAdapter(this, R.layout.member_row, c, from, to);
		lv.setAdapter(members);
	}
	
	private void writeMessage(long rowId) {
		Cursor c = mDb.getMyTeamMember(rowId);
		Intent intent = new Intent(MemberOverviewActivity.this, WriteMessageActivity.class);
		intent.putExtra("memberId", memberId);
		intent.putExtra("senderid", c.getString(c.getColumnIndex(DBAdapter.KEY_MEMBERID)));
		intent.putExtra("message", "");
		startActivity(intent);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.menu_message_writemessage:
			writeMessage(info.id);
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
			super.onCreateContextMenu(menu, v, menuInfo);
			menu.clear();
		
		if (!memberId.equals("")) {
			menu.add(0, R.id.menu_message_writemessage, 0, "Skriv meddelande");
		} 
		
	}
}
